package com.zoo.diannaopeijian.service.impl;

import com.zoo.diannaopeijian.domain.Diannaopeijian;
import com.zoo.diannaopeijian.domain.DiannaopeijianExample;
import com.zoo.diannaopeijian.mapper.DiannaopeijianMapper;
import com.zoo.diannaopeijian.service.SysService;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

/**
 * Created by zx583 on 2018-02-04.
 */
@Service
public class SysServiceImpl implements SysService {
    @Resource
    private DiannaopeijianMapper sysMapper;

    @Override
    public boolean updateSysName(String sysname) {
        List<Diannaopeijian> diannaopeijians = sysMapper.selectByExample(new DiannaopeijianExample());

        if (diannaopeijians == null || diannaopeijians.size() == 0) {
            return insertSysName(sysname);
        }

        Diannaopeijian newSys = new Diannaopeijian();
        newSys.setSysname(sysname);

        return sysMapper.updateByExample(newSys, new DiannaopeijianExample()) == 1;
    }

    @Override
    public boolean insertSysName(String sysname) {
        Diannaopeijian diannaopeijian = new Diannaopeijian();
        diannaopeijian.setSysname(sysname);

        return sysMapper.insert(diannaopeijian) == 1;
    }

    @Override
    public String getSysName() {
        List<Diannaopeijian> diannaopeijians = sysMapper.selectByExample(new DiannaopeijianExample());

        if (diannaopeijians == null || diannaopeijians.size() == 0) {
            return null;
        }

        return diannaopeijians.get(0).getSysname();
    }

    @Override
    public boolean updateLogo(MultipartFile logoUpload) {
        if (logoUpload.isEmpty()) {
            return false;
        }

        try {
            logoUpload.transferTo(ResourceUtils.getFile("classpath:static/images/logo.png"));
        } catch (FileNotFoundException e) {
            return false;
        } catch (IOException e) {
            return false;
        }

        return true;
    }
}
